package com.lottery.material.service.admin.portaluser.impl;

import com.lottery.material.dao.portaluser.BuyGoldConfigMapper;
import com.lottery.material.enumeration.common.CommonEnum;
import com.lottery.material.pojo.page.Page;
import com.lottery.material.pojo.portaluser.BuyGoldConfig;
import com.lottery.material.pojocustom.portaluser.BuyGoldConfigCustom;
import com.lottery.material.service.admin.portaluser.BuyGoldConfigService;
import com.lottery.material.util.CalendarUtil;
import com.lottery.material.util.EnumUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Description：充值金额配置服务接口实现类
 * Created by 朱正磊 on 2019-04-13 17:29
 */
@Slf4j
@Service("adminBuyGoldConfigService")
public class BuyGoldConfigServiceImpl implements BuyGoldConfigService {

    private final BuyGoldConfigMapper buyGoldConfigMapper;

    @Autowired
    public BuyGoldConfigServiceImpl(BuyGoldConfigMapper buyGoldConfigMapper) {
        this.buyGoldConfigMapper = buyGoldConfigMapper;
    }

    /**
     * description：新增一条数据
     * user 朱正磊
     * time 2019-04-20 15:14
     * @param buyGoldConfig 对象
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer add(BuyGoldConfig buyGoldConfig) {
        buyGoldConfig.setCreateTime(new Date());
        buyGoldConfig.setIsDelete((Integer) CommonEnum.IS_DELETE_FALSE.getValue()); //默认未逻辑删除
        return buyGoldConfigMapper.insertSelective(buyGoldConfig);
    }

    /**
     * description：根据主键物理删除
     * user 朱正磊
     * time 2019-04-20 15:13
     * @param id 主键
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer deleteById(Integer id) {
        return buyGoldConfigMapper.deleteByPrimaryKey(id);
    }

    /**
     * description：根据主键查询唯一数据
     * user 朱正磊
     * time 2019-04-20 15:15
     * @param id 主键
     * @return 返回唯一数据
     */
    @Override
    public BuyGoldConfig findById(Integer id) {
        return buyGoldConfigMapper.selectByPrimaryKey(id);
    }

    /**
     * description：根据主键修改一条数据
     * user 朱正磊
     * time 2019-04-20 15:16
     * @param buyGoldConfig 对象
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer updateById(BuyGoldConfig buyGoldConfig) {
        return buyGoldConfigMapper.updateByPrimaryKeySelective(buyGoldConfig);
    }

    /**
     * description：查询扩展类分页列表
     * user 朱正磊
     * time 2019-04-17 17:01
     * @param page 分页对象
     * @return 返回扩展类分页列表数据
     */
    @Override
    public List<BuyGoldConfigCustom> findCustomListPage(Page page) {
        List<BuyGoldConfigCustom> list = buyGoldConfigMapper.findCustomListPage(page);
        list.forEach(this::setCustom);
        return list;
    }

    /**
     * description：根据主键查询唯一扩展类数据
     * user 朱正磊
     * time 2019-04-17 17:03
     * @param id 主键
     * @return 返回唯一扩展类数据
     */
    @Override
    public BuyGoldConfigCustom findCustomById(Integer id) {
        BuyGoldConfigCustom custom = buyGoldConfigMapper.findCustomById(id);
        this.setCustom(custom);
        return custom;
    }

    /**
     * description：设置扩展类的扩展属性
     * user 朱正磊
     * time 2019-04-17 17:48
     * @param custom 扩展类对象
     */
    private void setCustom(BuyGoldConfigCustom custom) {
        //创建时间
        custom.setCreateTimeFormat(CalendarUtil.dateToString(custom.getCreateTime()));
        //是否删除（1：否；2：是）
        custom.setIsDeleteDesc(EnumUtil.getDescriptionByCodeValue(CommonEnum.IS_DELETE, custom.getIsDelete(), CommonEnum.class));
    }

    /**
     * description：根据主键删除一条数据（逻辑删除）
     * user 朱正磊
     * time 2019-04-30 11:30
     * @param id 主键
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer deleteLogicById(Integer id) {
        BuyGoldConfig buyGoldConfig = new BuyGoldConfig();
        buyGoldConfig.setId(id);
        buyGoldConfig.setIsDelete((Integer) CommonEnum.IS_DELETE_TRUE.getValue());
        Integer flag = buyGoldConfigMapper.updateByPrimaryKeySelective(buyGoldConfig);
        log.info("【逻辑删除持久化操作数据库状态：flag = {}】", flag);
        return flag;
    }

    /**
     * （可以不存在，如果存在只允许存在一条数据）（未逻辑删除）
     * description：根据充值金额查询列表数据（如果主键不为null，查询的时候排除掉该记录）
     * user 朱正磊
     * time 2019-05-15 11:16
     * @param id 主键（新增为null，修改不为null）
     * @param buyGold 充值金额
     * @return 返回列表数据
     */
    @Override
    public List<BuyGoldConfig> selectByBuyGold(Integer id, Integer buyGold) {
        return buyGoldConfigMapper.selectByBuyGold(id, buyGold);
    }

    /**
     * description：查询没有被逻辑删除的数据
     * user 朱正磊
     * time 2019-05-15 14:21
     * @return 返回没有被逻辑删除的数据集合列表
     */
    @Override
    public List<BuyGoldConfig> selectNotBeDeleted() {
        return buyGoldConfigMapper.selectNotBeDeleted();
    }
    
}
